<?php
/**
 * @version $Id: admin.sketchup.php,v 1.1 2009/06/03 00:36:07 dgravato Exp $
 * @package    sketchup
 * @subpackage 
 * @author     Chris Borkowski {@link http://www.guggenheim.org}
 * @author     Created on 30-Mar-2009
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


require_once( JApplicationHelper::getPath( 'admin_html' ) );
JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');

switch($task)
{
	case 'edit':
	case 'add':
	editSketchup( $option );
		break;
	
	case 'apply':
	case 'save':
	saveSketchup( $option, $task );
		break;
	
	case 'remove':
	removeSketchups( $option );
		break;
	
	default: 
	showSketchups( $option ); 
		break; 	
}

function showSketchups( $option ) 
{ 
	$db =& JFactory::getDBO(); 
	$query = "SELECT * FROM #__sketchup"; 
	$db->setQuery( $query ); 
	$rows = $db->loadObjectList(); 

	if ($db->getErrorNum()) { 
		echo $db->stderr(); 
		return false; 
	} 

	HTML_sketchups::showSketchups( $option, $rows ); 
} 



function editSketchup( $option )
{
	$row =& JTable::getInstance('sketchup', 'Table'); 
	$cid = JRequest::getVar( 'cid', array(0), '', 'array' ); 
	$id = $cid[0]; 
	$row->load($id);
	
	$lists = array();
	$lists['finalist'] = JHTML::_('select.booleanlist', 'finalist', 'class="inputbox"', $row->finalist);
	$lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $row->published);
	
	HTML_sketchups::editSketchup($row, $lists, $option);
}

function saveSketchup( $option, $task )
{
	global $mainframe;
	
	JRequest::checkToken() or die( 'Invalid Token' );
	
	$row =& JTable::getInstance('sketchup', 'Table');
	
	if (!$row->bind(JRequest::get('post')))
	{
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	
	$row->p_description = JRequest::getVar( 'p_description', '', 'post', 'string', JREQUEST_ALLOWRAW );

	$date =& JFactory::getDate($row->sub_date);
	$row->sub_date = $date->toMySQL();
	
	if (!$row->store())
	{
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	
	switch ($task)
	{
		case 'apply':
			$msg = 'Changes to Sketchup Entry Saved';
			$link = 'index.php?option=' . $option . '&task=edit&cid[]='. $row->id;
			break;

		case 'save':
		default:
			$msg = 'Sketchup Entry Saved'; 
			$link = 'index.php?option=' . $option; 
			break; 
	}
	
	$mainframe->redirect($link, $msg);
}

function removeSketchups( $option )
{
	global $mainframe;
	
	JRequest::checkToken() or die( 'Invalid Token' );
	
	$cid = JRequest::getVar( 'cid', array(), '', 'array' );
	$db =& JFactory::getDBO();

	if(count($cid))
	{
		JArrayHelper::toInteger($cid);
		
		$cids = implode( ',', $cid );
		$query = "DELETE FROM #__sketchup WHERE id IN ( $cids )";
		$db->setQuery( $query );
		
		if (!$db->query())
		{
		   echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n";
		}
	}
	
	$mainframe->redirect( 'index.php?option=' . $option );
}

function createThubnail( $upload, $newWidth, $name )
     {
          if (!$info = getimagesize($upload) )
               return false;
          //echo "width: "  . $info[0] . "<br/>";
          //echo "height: " . $info[1] . "<br/>";

          //create crude aspect ratio:
          $aspect = $info[0] / $info[1];
          $newHeight = round( $newWidth/$aspect );
          //echo "$newHeight <br/>";

          $src = @imagecreatefromjpeg("$upload");
          if ( !$src )
               return false;

          $tmp = @imagecreatetruecolor( $newWidth, $newHeight );
          imagecopyresampled( $tmp, $src, 0, 0, 0, 0, $newWidth,
                                         $newHeight, $info[0], $info[1] );

          imagejpeg( $tmp, $name, 100 );
          imagedestroy( $src );
          imagedestroy( $tmp );
          return true;
     }

?>
